package com.example.aigc_education.controller;

import com.example.aigc_education.anno.RequestKeyParam;
import com.example.aigc_education.anno.RequestLock;
import com.example.aigc_education.common.BusinessException;
import com.example.aigc_education.domain.Resources;
import com.example.aigc_education.domain.dto.ResourcesDTO;
import com.example.aigc_education.domain.vo.ResourcesVO;
import com.example.aigc_education.service.ResourcesService;
import com.example.aigc_education.utils.ResultUtil;
import com.example.aigc_education.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Resource
    private ResourcesService resourcesService;


    /**
     * 修改总结事件
     *
     * @param resourcesDTO  总结dto对象，包含 resourcesId 和修改之后的 summary 内容
     * @return      操作成功、失败信息
     */
    @RequestLock(prefix = "updateSummary", expire = 3)
    @PutMapping("/updateSummary")
    public ResultUtil updateSummary(@RequestKeyParam @RequestBody ResourcesDTO resourcesDTO){
        try{
            Resources resources = resourcesService.getById(resourcesDTO.getResourceId());
            resources.setSummary(resourcesDTO.getSummary());
            resourcesService.updateById(resources);

            return ResultUtil.success("修改web资源总结成功");
        }catch (Exception e){
            throw new BusinessException("修改web资源总结失败");
        }

    }


    /**
     * 查看resources资源，可以查看链接
     *
     * @param themeId   主题id
     * @return          操作成功返回一个resourcesVO对象
     */
    @GetMapping("/selectResources")
    public ResultUtil selectResources(Long themeId){
        ResourcesVO resourcesVO = resourcesService.getResources(themeId);
        // 当不存在resources时，返回null
        if(resourcesVO == null){
            Object data = "null";
            return ResultUtil.success(data);
        }

        return ResultUtil.success(resourcesVO);
    }


    /**
     * 添加 web资源链接
     * @param resourcesDTO  web资源DTO对象
     * @return              添加成功信息
     */
    @RequestLock(prefix = "addLearningLink", expire = 3)
    @PostMapping("/addLink")
    public ResultUtil addLearningLink(@RequestKeyParam @RequestBody ResourcesDTO resourcesDTO){
        resourcesService.addLearningLink(resourcesDTO);

        return ResultUtil.success("添加web资源链接成功");
    }


    /**
     * 保存学习资源
     * @param resourcesVO   学习资源的VO对象
     * @return              保存成功的信息
     */
    @RequestLock(prefix = "saveResource", expire = 3)
    @PostMapping("saveResources")
    public ResultUtil saveResources(@RequestKeyParam @RequestBody ResourcesVO resourcesVO){
        // 判断VO是否为空
        if( resourcesVO != null)
            resourcesService.saveResources(resourcesVO);
        else
            return ResultUtil.error("保存学习资源不能为空");


        return ResultUtil.success("保存学习资源成功");
    }

    /**
     * 通过学习资源id查找学习资源
     * 当学习资源id为空时候，或者学习资源为null时候，报错误：找不到学习资源
     *
     * @param id    资源id
     * @return      成功时候返回Resources，错误时候返回错误信息
     */
    @RequestLock(prefix = "getResources", expire = 3)
    @GetMapping("/getResources")
    public ResultUtil getResources(@RequestKeyParam @RequestParam("id")Long id){

        ResourcesVO resourcesVO = resourcesService.getResourcesById(id);
        // 当学习资源为空时候
        if (StringUtils.isNull(resourcesVO))
            return ResultUtil.success("找不到该学习资源");

        return ResultUtil.success(resourcesVO);
    }
}
